﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式之禅.责任链模式
{
    /// <summary>
    /// 抽象处理者类
    /// </summary>
    public abstract class AbstractHandler
    {
        public AbstractHandler NextHandler { get; set; }
        public abstract string ProcessRequest(int requestObj);
    }
    /// <summary>
    /// 具体处理类A 只处理小于10
    /// </summary>
    public class HandlerA : AbstractHandler
    {
        public override string ProcessRequest(int requestObj)
        {
            if (requestObj < 10)
            {
                return "HandlerA";
            }
            else if (NextHandler != null)
            {
                return NextHandler.ProcessRequest(requestObj);
            }
            else
                return "HandlerA Stop";
        }
    }
    /// <summary>
    /// 具体处理类B 只处理小于100
    /// </summary>
    public class HandlerB : AbstractHandler
    {
        public override string ProcessRequest(int requestObj)
        {
            if (requestObj < 100)
            {
                return "HandlerB";
            }
            else if (NextHandler != null)
            {
                return NextHandler.ProcessRequest(requestObj);
            }
            else
                return "HandlerB Stop";
        }
    }

    /// <summary>
    /// 具体处理类C 只处理小于1000
    /// </summary>
    public class HandlerC : AbstractHandler
    {
        public override string ProcessRequest(int requestObj)
        {
            if (requestObj < 1000)
            {
                return "HandlerC";
            }
            else if (NextHandler != null)
            {
                return NextHandler.ProcessRequest(requestObj);
            }
            else
                return "HandlerC Stop";
        }
    }

    public class Client
    {
        public string HandlerRequest(int requestObj)
        {
            AbstractHandler handlerA = new HandlerA();
            AbstractHandler handlerB = new HandlerB();
            AbstractHandler handlerC = new HandlerC();

            handlerA.NextHandler = handlerB;
            handlerB.NextHandler = handlerC;

            return handlerA.ProcessRequest(requestObj);
        }
    }

}
